﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using System.Data.SqlClient;

namespace LotteryVote.Data.Drop
{
    public class BetUpperLimitDataAccess : DataBase
    {
        public void Insert(BetUpperLimit upperLimit)
        {
            string sql = string.Format(@"INSERT INTO {0} ({1},{2},{3},{4},{5},{6},{7}) VALUES (@{1},@{2},@{3},@{4},@{5},@{6},@{7}) 
SELECT IDENT_CURRENT('{0}')", BetUpperLimit.TABLENAME, BetUpperLimit.NUM, BetUpperLimit.COMPANYID, BetUpperLimit.GAMEPLAYWAYID, BetUpperLimit.DROPVALUE,
                            BetUpperLimit.NEXTLIMIT, BetUpperLimit.UPPERLLIMIT, BetUpperLimit.TOTALBETAMOUNT);
            object id = base.ExecuteScalar(sql, new SqlParameter(BetUpperLimit.NUM, upperLimit.Num),
                new SqlParameter(BetUpperLimit.COMPANYID, upperLimit.CompanyId),
                new SqlParameter(BetUpperLimit.GAMEPLAYWAYID, upperLimit.GamePlayWayId),
                new SqlParameter(BetUpperLimit.DROPVALUE, upperLimit.DropValue),
                new SqlParameter(BetUpperLimit.NEXTLIMIT, upperLimit.NextLimit),
                new SqlParameter(BetUpperLimit.UPPERLLIMIT, upperLimit.UpperLlimit),
                new SqlParameter(BetUpperLimit.TOTALBETAMOUNT, upperLimit.TotalBetAmount));
            upperLimit.LimitId = Convert.ToInt32(id);
        }

        public void Update(BetUpperLimit upperLimit)
        {
            string sql = string.Format(@"UPDATE {0} SET {1}=@{1},{2}=@{2},{3}=@{3},{4}=@{4} WHERE {5}=@{5}", BetUpperLimit.TABLENAME,
                BetUpperLimit.DROPVALUE, BetUpperLimit.NEXTLIMIT, BetUpperLimit.UPPERLLIMIT, BetUpperLimit.TOTALBETAMOUNT, BetUpperLimit.LIMITID);
            base.ExecuteNonQuery(sql, new SqlParameter(BetUpperLimit.DROPVALUE, upperLimit.DropValue),
                new SqlParameter(BetUpperLimit.NEXTLIMIT, upperLimit.NextLimit),
                new SqlParameter(BetUpperLimit.UPPERLLIMIT, upperLimit.UpperLlimit),
                new SqlParameter(BetUpperLimit.TOTALBETAMOUNT, upperLimit.TotalBetAmount),
                new SqlParameter(BetUpperLimit.LIMITID, upperLimit.LimitId));
        }
        public void UpdateBetStatus(int limitId, bool status)
        {
            string sql = string.Format(@"UPDATE {0} SET {1}=@{1} WHERE {2}=@{2}", BetUpperLimit.TABLENAME, BetUpperLimit.STOPBET, BetUpperLimit.LIMITID);
            base.ExecuteNonQuery(sql, new SqlParameter(BetUpperLimit.STOPBET, status),
               new SqlParameter(BetUpperLimit.LIMITID, limitId));
        }

        public BetUpperLimit GetUpperLimit(int limitId)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1}", BetUpperLimit.TABLENAME, BetUpperLimit.LIMITID);
            return base.ExecuteEntity<BetUpperLimit>(sql, new SqlParameter(BetUpperLimit.LIMITID, limitId));
        }
        public IList<BetUpperLimit> ListUpperLimitByDate(DateTime date)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE DAY({1})=DAY(@{1})", BetUpperLimit.TABLENAME, BetUpperLimit.CREATETIME);
            return base.ExecuteList<BetUpperLimit>(sql, new SqlParameter(BetUpperLimit.CREATETIME, date));
        }
        public IList<BetUpperLimit> ListUpperLimitByCondition(DateTime date, string num, int companyId, int gameplayway)
        {
            List<SqlParameter> parameterList = new List<SqlParameter>();
            StringBuilder sb = new StringBuilder();
            Action<string, object> appendToCondition = (key, value) =>
            {
                if (sb.Length > 0)
                    sb.Append(" AND ");
                sb.AppendFormat("{0}=@{0}", key);
                parameterList.Add(new SqlParameter(key, value));
            };
            sb.AppendFormat("DAY({0})=DAY(@{0})", BetUpperLimit.CREATETIME);
            parameterList.Add(new SqlParameter(BetUpperLimit.CREATETIME, date));
            if (!string.IsNullOrEmpty(num))
                appendToCondition(BetUpperLimit.NUM, num);
            if (companyId > 0)
                appendToCondition(BetUpperLimit.COMPANYID, companyId);
            if (gameplayway > 0)
                appendToCondition(BetUpperLimit.GAMEPLAYWAYID, gameplayway);

            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}", BetUpperLimit.TABLENAME, sb.ToString());
            return base.ExecuteList<BetUpperLimit>(sql, parameterList.ToArray());
        }
    }
}
